//
//  AppDelegate+EaseMob.m
//  XJL_WeChat
//
//  Cre/Volumes/学习/.TemporaryItemsated by XJL on 16/8/2.
//  Copyright © 2016年 Xujiali. All rights reserved.
//

#import "AppDelegate+EaseMob.h"
#import "XJLLoginViewController.h"

@implementation AppDelegate (EaseMob)

#pragma mark - 聊天管理器的代理

/*!
 @method
 @brief 用户将要进行自动登录操作的回调
 @discussion
 @param loginInfo 登录的用户信息
 @param error     错误信息
 @result
 */
- (void)willAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error{
    
}

#pragma mark 自动登录的回调
/*!
 @method
 @brief 用户自动登录完成后的回调
 @discussion
 @param loginInfo 登录的用户信息
 @param error     错误信息
 @result
 */
- (void)didAutoLoginWithInfo:(NSDictionary *)loginInfo error:(EMError *)error{
    if (!error) {
        XJLLog(@"自动登录成功");
    }else{
        XJLLog(@"自动登录失败");
    }
    
}

#pragma mark 在其它设备登录,被强迫退出
- (void)didLoginFromOtherDevice{
    //设置自动登录状态为NO
   [[EaseMob sharedInstance].chatManager setIsAutoLoginEnabled:NO];
    
    //退出,如果在其他设备上登录，自己调用退出方法
    [[EaseMob sharedInstance].chatManager asyncLogoffWithUnbindDeviceToken:YES];
    //1.回到登录界面
    self.window.rootViewController = [[XJLLoginViewController alloc]init];
    //给用户提示
    [self showAlert:@"登录提示" msg:@"当前登录账号在其它设备登录"];
    
}

-(void)showAlert:(NSString *)title msg:(NSString *)msg{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"知道了" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action];
    [self.window.rootViewController presentViewController:alert animated:YES completion:nil];
}


/*!
 @brief 好友请求被接受
 */
- (void)didAcceptedByBuddy:(NSString *)username{
    NSString *msg = [NSString stringWithFormat:@"你被%@接受了",username];
    [self showAlert:@"好友添加" msg:msg];
}

/*
 @brief 好友请求被拒绝时的回调
 */
- (void)didRejectedByBuddy:(NSString *)username{
    NSString *msg = [NSString stringWithFormat:@"你被%@拒绝了",username];
    [self showAlert:@"好友添加" msg:msg];
}

/**
 * 监听到好友的添加请求
 * 只要请求没有处理,程序每次启动会调用代理方法
 **/

//#warning 添加好友请求。。。
-(void)didReceiveBuddyRequest:(NSString *)username message:(NSString *)message{
    NSUserDefaults *defults = [NSUserDefaults standardUserDefaults];
    XJLLog(@"%@,%@",username,message);
    //数据没有传过来，为了防止崩溃，加了一个判断
//    if (message == nil) {
//        message = @"你好";
//    }
    // 1.先从本地 用户偏好设置 获取 以前的数据
    NSArray *oldBuddyRequests = [defults objectForKey:@"BuddyRequest"];
    XJLLog(@"%@",oldBuddyRequests);
    BOOL isUserExist = NO;//用户是否已经存在
    for (NSDictionary *oldBuddyRequest in oldBuddyRequests) {
        if ([oldBuddyRequest[@"username"] isEqualToString:username]) {
            isUserExist = YES;
            break;
        }
    }
    
    if (isUserExist) {
        return;
    }
    
    //把好友的添加请求 保存到 用户偏好设置
    NSMutableArray *newBuddyRequests = [NSMutableArray array];
    //添加旧的
    [newBuddyRequests addObjectsFromArray:oldBuddyRequests];
    XJLLog(@"%@",oldBuddyRequests);
    XJLLog(@"%@",newBuddyRequests);
    
    //添加新的
    if (newBuddyRequests.count != 0) {
        
        [newBuddyRequests addObject:@{@"username":username,@"message":message}];
        [defults setObject:newBuddyRequests forKey:@"BuddyRequest"];
        //用synchronize方法把数据持久化到standardUserDefaults数据库
        [defults synchronize];
    }
//    [newBuddyRequests addObject:@{@"username":username,@"message":message}];
//    [defults setObject:newBuddyRequests forKey:@"BuddyRequest"];
//    //用synchronize方法把数据持久化到standardUserDefaults数据库
//    [defults synchronize];
    
    //设置联系人UITabBarBtn的数字提示
    
    [self showUdhandleBuddyRequestCount];
    
}

#pragma mark 显示未处理的好友请求数
-(void)showUdhandleBuddyRequestCount{
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    // 先从本地 用户偏好设置 获取 以前的数据
    NSArray *buddyRequests = [defaults objectForKey:@"BuddyRequest"];
    
    // 设置联系人UITabBarBtn的数字提示
    UITabBarController *tabVc = (UITabBarController *)self.window.rootViewController;
    UINavigationController *nav = tabVc.childViewControllers[1];
    if (buddyRequests.count > 0) {
        nav.tabBarItem.badgeValue = [NSString stringWithFormat:@"%ld",buddyRequests.count];
    }
   }

@end
